angular.
module('otherAccount').
component('otherAccount', {
  templateUrl:
  'views/app/other-account/other-account.template.html',
  controller: function ($scope,$cookies,$http,$window,$location,$rootScope) {

    $scope.accountList = []
    // if ($cookies.get('sessionID') == undefined){
    //   $window.location = '/login'
    // }
    name = ''

    $http({
      method: 'get',
      url: '/admin/getOtherAccount',
  }).success(function(datax) {
    if(datax != undefined) {
      // for (var i  in datax.data){
       $scope.accountList = datax.data
      // }
    }
  })


    $scope.addType = function(){
      $scope.accountList.push({'name':'','uid':''})
    }

    $scope.remove = function(index){
      // $scope.activityTypeList.splice(index,1)
      if ($scope.accountList[index]._id == undefined){
        $scope.accountList.splice(index,1)
        return 
      }
       $http({
              method: 'post',
              url: '/admin/removeOtherAccount',
              data : {id:$scope.accountList[index]._id,session:$cookies.get('sessionID')}

            }).success(function(datax) {
              if (datax.code == 200){
                $scope.accountList.splice(index,1)
                 swal('删除成功', '', "success").then(function(){
              })   
              }
              if (datax.code == 401){
                $window.location = '/login'
              }
                  
                })  
      }
    

    $scope.newOtherAccount = function(){
      var data = {}
      data.session = $cookies.get('sessionID')

      data.methods = JSON.stringify($scope.accountList)
      console.log(data,'dsdsd')
       $http({
              method: 'post',
              url: '/admin/saveOtherAccount',
              data : data

            }).success(function(datax) {
              if (datax.code == 200){
                    $http({
      method: 'get',
      url: '/admin/getOtherAccount',
  }).success(function(datax) {
    if(datax != undefined) {
      // for (var i  in datax.data){
       $scope.accountList = datax.data
      // }
    }
  })
                 swal('保存成功', '', "success").then(function(){
              })   
              }
              if (datax.code == 401){
                $window.location = '/login'
              }
                  
                })          
          }
      
    
}})



